package com.zhongbei.controller;

import com.zhongbei.pojo.Member;
import com.zhongbei.service.MemberService;
import com.zhongbei.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @PostMapping
    public Result<String> create(@RequestBody Member member) {
        int i = memberService.insertMember(member);
        return i == 1 ? Result.success("新增成功") : Result.error("新增失败");
    }

    @DeleteMapping("/{memberId}")
    public Result<String> delete(@PathVariable Integer memberId) {
        int i = memberService.deleteMemberByMemberId(memberId);
        return i == 1 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PutMapping("/{memberId}")
    public Result<String> update(@PathVariable Integer memberId, @RequestBody Member member) {
        member.setMemberId(memberId);
        int i = memberService.updateMemberByMemberId(member);
        return i == 1 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("/id/{memberId}")
    public Result<List<Member>> getById(@PathVariable Integer memberId) {
        return Result.success(memberService.selectMemberByMemberId(memberId));
    }

    @GetMapping("/name/{memberName}")
    public Result<List<Member>> getByName(@PathVariable String memberName) {
        return Result.success(memberService.selectMemberByMemberName(memberName));
    }
}
